﻿/*    
    This file is part of d-mixr.

    d-mixr is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    d-mixr is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with d-mixr.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;

namespace Dmixr
{
    class MidiSystem
    {
        private Midi.InputDevice m_inputDevice = null;
        private Midi.OutputDevice m_outputDevice = null;
        private static MidiSystem m_instance;
        private List<MidiBinding> m_midiBindings = new List<MidiBinding>();

        public Midi.InputDevice InputDevice
        {
            get
            {
                return m_inputDevice;
            }
            set
            {
                // Destroy midi input
                if (m_inputDevice != null)
                {
                    m_inputDevice.ControlChange -= MidiInputControlChange;
                    m_inputDevice.StopReceiving();
                    m_inputDevice.Close();
                }

                m_inputDevice = value;
                //Create midi input
                if (m_inputDevice != null)
                {
                    m_inputDevice.Open();
                    m_inputDevice.ControlChange += MidiInputControlChange;
                    m_inputDevice.StartReceiving(null); 
                }
            }
        }

        public Midi.OutputDevice OutputDevice
        {
            get
            {
                return m_outputDevice;
            }
            set
            {
                if (m_outputDevice != null)
                {
                    m_outputDevice.Close();
                }

                m_outputDevice = value;

                if (m_outputDevice != null)
                {
                    m_outputDevice.Open();
                }
            }
        }

        void MidiInputControlChange(Midi.ControlChangeMessage msg)
        {
            foreach (MidiBinding m in m_midiBindings.FindAll(b => b.Channel == (int)msg.Channel && b.ControlChange == (int)msg.Control))
            {
                if (m.DataChanged != null)
                    m.DataChanged(msg.Value);
            }
        }

        private MidiSystem()
        {
        }
        
        public static MidiSystem Instance
        {
            get
            {
                if (m_instance == null)
                {
                    m_instance = new MidiSystem();
                }
                return m_instance;
            }
        }

        public void AddMidiBinding(MidiBinding b)
        {
            m_midiBindings.Add(b);
        }

        public void RemoveMidiBinding(MidiBinding b)
        {
            m_midiBindings.Remove(b);
        }
    }
}
